__all__ = [
    "load_config",
    "Deployment",
    "Pod",
    "Service",
    "Ingress",
    "Node",
    "AutoScaler",
    "Kubectl",
    "deployment_yaml_template",
    "service_yaml_template",
    "secret_yaml_template",
    "ingress_yaml_template",
    # ----------------------------
    "Package",
    "Function",
    "Environment",
    "HTTPTrigger",
    "Fissionctl",
    "package_yaml_template",
    "environment_yaml_template",
    "function_yaml_template",
    "fission_yaml_template"
    # ----------------------------

]

from .k8s import Kubectl, load_config
from .k8s import Deployment, Pod, Service, Ingress, AutoScaler, Node
import yaml
import json
from .fission import Package, Function, Environment, HTTPTrigger, Fissionctl


class K8sYaml:

    def __init__(self):
        """
        """
        self.body = []

    def read_file(self, filename):
        with open(filename, "r") as f:
            return f.read()

    def add_resource(self, stream: str, **kwargs):
        self.body.extend(yaml.safe_load_all(stream=stream))

    def print_json(self, filename: str = None):
        if filename:
            with open(filename, "w") as f:
                f.write(json.dumps(self.body, indent=4, ensure_ascii=False))
        else:
            print(json.dumps(self.body, indent=4, ensure_ascii=False))

    def print_yaml(self, filename: str = None):
        if filename:
            with open(filename, "w") as f:
                f.write(yaml.safe_dump_all(self.body))
        else:
            print(yaml.safe_dump_all(self.body))


deployment_yaml_template = """
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: { app }
name: { name }
spec:
replicas: 1
selector:
matchLabels:
app: { app }
yaml_template:
metadata:
annotations:
prometheus.io/path: /metrics
prometheus.io/port: '80'
prometheus.io/scrape: 'true'
labels:
app: { app }
spec:
containers:
- env:
- name: { env0_name }
  value: { env0_value }
- name: { env1_name }
  value: { env1_value }
image: { image }
imagePullPolicy: { imagePullPolicy }
name: { name }
ports:
- containerPort: { ports0_containerPort }
resources:
limits:
  cpu: { cpu_limits }
  memory: { memory_limits }
requests:
  cpu: { cpu_requests }
  memory: { memory_requests }
volumeMounts:
- mountPath: /var/run/docker.sock
  name: vol1
imagePullSecrets:
- name: { imagePullSecrets }
volumes:
- hostPath:
path: /var/run/docker.sock
type: Socket
name: { volumes0_name }
"""

namespace_yaml_template = """
apiVersion: v1
kind: Namespace
metadata:
name:
"""

service_yaml_template = """
apiVersion: v1
kind: Service
metadata:
labels:
app: { app }
name: { name }
spec:
ports:
- name: { spec_ports0_name }
port: { spec_ports0_port }
protocol: TCP
targetPort: { spec_ports0_port }
selector:
app: { spec_selector_app }
"""

ingress_yaml_template = """
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: { name }
spec:
rules:
- host: { host }
http:
paths:
- backend:
  serviceName: { serviceName }
  servicePort: { servicePort }
path: /
"""

secret_yaml_template = """
apiVersion: v1
kind: Secret
metadata:
name: secret-dockercfg
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: xxxx
"""
# ------------------------------------------
fission_yaml_template = """
apiVersion: fission.io/v1
kind: Environment
metadata:
name: python-27
namespace: default
spec:
version: 2
builder:
command: build
image: fission/python-builder
runtime:
image: fission/python-env

---
apiVersion: fission.io/v1
kind: Function
metadata:
name: hello
namespace: default
spec:
InvokeStrategy:
ExecutionStrategy:
ExecutorType: poolmgr
MaxScale: 0
MinScale: 0
SpecializationTimeout: 120
TargetCPUPercent: 0
StrategyType: execution
environment:
name: python-27
namespace: default
package:
packageref:
name: hello-pkg
namespace: default
functionName: hello.main

---
apiVersion: fission.io/v1
kind: Package
metadata:
name: hello-pkg
namespace: default
spec:
source:
url: archive://hello-archive
buildcmd: "./build.sh"
environment:
name: python-27
namespace: default
status:
buildstatus: pending

---
kind: ArchiveUploadSpec
name: hello-archive
include:
- "hello/*"
"""

environment_yaml_template = """
apiVersion: fission.io/v1
kind: Environment
metadata:
  name: python-27
  namespace: default
spec:
  version: 2
  builder:
    command: build
    image: fission/python-builder
  runtime:
    image: fission/python-env
"""

function_yaml_template = """
apiVersion: fission.io/v1
kind: Function
metadata:
  name: hello
  namespace: default
spec:
  InvokeStrategy:
    ExecutionStrategy:
      ExecutorType: poolmgr
      MaxScale: 0
      MinScale: 0
      SpecializationTimeout: 120
      TargetCPUPercent: 0
    StrategyType: execution
  environment:
    name: python-27
    namespace: default
  package:
    packageref:
      name: hello-pkg
      namespace: default
    functionName: hello.main
"""

package_yaml_template = """
apiVersion: fission.io/v1
kind: Package
metadata:
  name: hello-pkg
  namespace: default
spec:
  source:
    url: archive://hello-archive
  buildcmd: "./build.sh"
  environment:
    name: python-27
    namespace: default
status:
  buildstatus: pending

---
kind: ArchiveUploadSpec
name: hello-archive
include:
  - "hello/*"
"""
